package com.example.fly.pushdemo.smack;

import com.example.fly.pushdemo.smack.extension.CloseExt;
import com.example.fly.pushdemo.smack.extension.ErrorExt;
import com.example.fly.pushdemo.smack.extension.GoodsPutExt;
import com.example.fly.pushdemo.smack.extension.GoodsTakeExt;
import com.example.fly.pushdemo.smack.extension.QrcodeExt;
import com.orhanobut.logger.Logger;


import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IqMessage;
import org.jivesoftware.smack.packet.Message;

import xmpp.androidpush.aidl.PushBinder;
import xmpp.androidpush.aidl.PushIqMessage;
import xmpp.androidpush.aidl.PushMessage;

/**
 * Created by fly on 2017/6/6.
 */

public class SendMsg {

    private PushBinder mPushBinder;
    private String  mDeviceId;

    public SendMsg(String deviceId,PushBinder mPushBinder) {
        this.mDeviceId=deviceId;
        this.mPushBinder=mPushBinder;
    }
    public void sendOkMsg(){
        IqMessage msg=new IqMessage();
        msg.setFrom(mDeviceId+"@dev.xlbyun.cn");
        msg.setTo("dev.xlbyun.cn");
        msg.setPacketID(mDeviceId);
        msg.setType(IQ.Type.RESULT);

        PushMessage pushMessage =new PushMessage(msg);
        mPushBinder.sendMessage(pushMessage);
    }

    public void sendErrorMsg(int errorCode,String errorMsg){
        IqMessage msg=new IqMessage();
//
////        上位机开门
//        msg.setFrom(mDeviceId+"@dev.xlbyun.cn");
//        msg.setTo("dev.xlbyun.cn");
//
//        msg.setId(mDeviceId);
//        msg.setType(IqMessage.Type.error);
//        ErrorExt ext=new ErrorExt();
//        ext.setCode(errorCode);
//        ext.setErrorMsg(errorMsg);
//        msg.addExtension(ext);
//        PushMessage pushMessage =new PushMessage(msg);
//        mPushBinder.sendMessage(pushMessage);
    }
    public void sendTokenMsg(String token,String qrcode){

        IqMessage msg=new IqMessage();
        msg.setXmlns("jabber:client");
        msg.setPacketID(mDeviceId);
        msg.setType(IQ.Type.SET);

        QrcodeExt ext =new QrcodeExt();
        ext.setToken(token);
        ext.setQrCode(qrcode);
        msg.addExtension(ext);
//        Logger.v(ext.toXML());

        Logger.v(msg.toXML());

        PushIqMessage pushMessage =new PushIqMessage(msg);
//        PushIqMessage pushMessage =new PushIqMessage(msg.toXML());
        if(msg!=null){
            mPushBinder.sendIqMessage(pushMessage.toString());
        }


    }

    /*
    * pick up
    * */
    public void sendPickUpMsg(String token,int goodsNumber,int count){

        MessageClient msg=new MessageClient();
        msg.setId(mDeviceId);
        msg.setType("set");
        GoodsTakeExt goodsTakeExt =new GoodsTakeExt();
        msg.addExtension(goodsTakeExt);
        PushMessage pushMessage =new PushMessage(msg);
        mPushBinder.sendMessage(pushMessage);
    }


    public void sendPutDownMsg(String token,int goodsNumber,int count){

        MessageClient msg=new MessageClient();
        msg.setId(mDeviceId);
        msg.setType("set");
        GoodsPutExt goodsTakeExt =new GoodsPutExt();
        msg.addExtension(goodsTakeExt);
        PushMessage pushMessage =new PushMessage(msg);
        mPushBinder.sendMessage(pushMessage);
    }

    public void sendCloseMsg(String token,String ordernumber){

        MessageClient msg=new MessageClient();
        msg.setId(mDeviceId);
        msg.setType("set");
        CloseExt ext =new CloseExt();
        ext.setToken(token);
        ext.setOrdernumber(ordernumber);
        msg.addExtension(ext);
        PushMessage pushMessage =new PushMessage(msg);
        mPushBinder.sendMessage(pushMessage);
    }
}
